//
//  HomeViewController.m
//  KYProject
//
//  Created by Roman on 16/12/20.
//  Copyright © 2016年 Roman. All rights reserved.
//

#import "HomeViewController.h"
#import "WeatherView.h"
#import "FamousDoctorView.h"
#import "SDCycleScrollView.h"
#import "DiseaseFlowLayout.h"
#import "DiseaseCell.h"
#import "HomePublicController.h"
#import "CycleController.h"
#import "HomeProvinceController.h"
#import "HomeDiseaseController.h"
#import "LWButton.h"

static NSString *testCellId = @"cell";
static NSString *wheaherCellID = @"wheatherCellId";
static NSString *cycleCellID = @"cycleCellID";
static NSString *mingYiCellID = @"mingyi";
static NSString *functonCellID = @"functionCellID";


//刷新次数
static long steps;

@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,SDCycleScrollViewDelegate>
@property(nonatomic,weak) UICollectionView *collectionView;
@property (nonatomic, weak) UIView *cycleScrollView;
@property (nonatomic, weak) FamousDoctorView *doctorView;

@property (nonatomic , strong) CADisplayLink *timer;
@property (nonatomic, weak) UIImageView *imageView;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI1];
    
    //选择地点的通知接收
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLocation) name:@"clickHomeView" object:nil];
    
    
    [self getNotification];
    
    self.timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
    
    //加入循环
    [self.timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}



#pragma mark
#pragma mark - 监听方法的通知 ,跳转到省份界面
- (void)changeLocation {
    
    [self.navigationController pushViewController:[[HomeProvinceController alloc] init] animated:YES];
}


#pragma mark
#pragma mark - 视图将要加载的时候进行点击City 的赋值
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    _homeCityName = _weatherView.bt_location.titleLabel.text;
    
    [_weatherView setupLocationWithCity:_homeCityName];
    //切换地点.
   
    [_weatherView getWeatherDataWithCity:_homeCityName];
    
    [self.collectionView reloadData];

}



- (void)setupUI1
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
    
    self.title = @"马医一生";
    
    
    //创建scrollview
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:scrollView];
    
    //创建天气
    WeatherView *weatherView = [WeatherView weatherview];
    
    [scrollView addSubview:weatherView];
    
    //创建轮播器
    SDCycleScrollView *cyscrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(100, 100, 100, 100) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    
    
    //url数组
    NSArray *imageArray = @[
                            @"http://img.guahao.com/portal_upload/rollingnews/1453447883150.jpg",
                            @"http://img.guahao.com/portal_upload/mediafocus/1461826474435.jpg",
                            @"http://img.guahao.com/portal_upload/rollingnews/1461574027543.jpg"
                            ];
    
    cyscrollView.imageURLStringsGroup = imageArray;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(top)];

    [cyscrollView addGestureRecognizer:tap];
    
    [scrollView addSubview:cyscrollView];
    
    
    //创建名医通
    FamousDoctorView *doctor = [FamousDoctorView doctorView];
    
    [scrollView addSubview:doctor];
    
    //创建疾病类型
    DiseaseFlowLayout *layout = [[DiseaseFlowLayout alloc]init];
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    
    [scrollView addSubview:collectionView];
    
    [collectionView registerNib:[UINib nibWithNibName:@"DiseaseCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"disease"];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    collectionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
    //设置item与collectionView边框的间距
    collectionView.contentInset = UIEdgeInsetsMake(1, 0, 0.5, 0);
    collectionView.scrollEnabled = NO;
    
    
#pragma mark
#pragma mark - 界面上小按钮.
    LWButton *button = [[LWButton alloc]initWithFrame:CGRectMake(5, 100, 50, 50)];
    
    
    button.layer.cornerRadius = 5;
    
    //设置图片样式
    [button setBackgroundImage:[UIImage imageNamed:@"2.Assistive Touch"] forState:UIControlStateNormal];
    
    [[UIApplication sharedApplication].keyWindow addSubview:button];
    

    
    /** 约束 */
    
    //ScrollView的约束
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    //天气
    [weatherView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom);
        make.leading.trailing.equalTo(self.view);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(100);
    }];
    //轮播器约束:
    
    [cyscrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weatherView.mas_bottom);
        make.leading.trailing.equalTo(scrollView);
        make.height.mas_equalTo(200);
    }];
    
    
    //名医通
    [doctor mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cyscrollView.mas_bottom);
        make.leading.trailing.equalTo(scrollView);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
        make.height.mas_equalTo(80);
    }];
    
    //疾病
   
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(doctor.mas_bottom);
        make.left.right.equalTo(scrollView);
        make.bottom.equalTo(scrollView);
        make.height.mas_equalTo(300);
    }];
    
    _weatherView = weatherView;
    
    _cycleScrollView = cyscrollView;
    _doctorView= doctor;
    _collectionView = collectionView;

    
}


//- (void)getNotification
//{
//    //跳转到主界面
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(button:) name:@"button" object:nil];
//}
//- (void)button:(NSNotification *)sender
//{
//    HomeViewController *HomeVc = [[HomeViewController alloc]init];
//    [self.navigationController pushViewController:HomeVc animated:YES];
//}

//移除通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
- (void)top
{
    [self.navigationController pushViewController:[[CycleController alloc]init] animated:YES];
    NSLog(@"......");
}


#pragma mark
#pragma mark - 实现数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DiseaseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"disease" forIndexPath:indexPath];
    
    cell.count = indexPath.section * 2 + indexPath.item;
    
    cell.tag = indexPath.section * 2 + indexPath.item;
    
    cell.backgroundColor = [UIColor whiteColor];
    
    
    return cell;
}

#pragma mark
#pragma mark - collection View delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DiseaseCell *cell = (DiseaseCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    //如果点击最后一个cell
    if (cell.tag == 5) {
        //跳转到其他链接
        [self.navigationController pushViewController:[[HomePublicController alloc]init] animated:YES];
        NSLog(@"实现了点击");
    }
    else
    {
        HomeDiseaseController *vc = [[HomeDiseaseController alloc]init];
        
        DiseaseCell *cell = (DiseaseCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        vc.diseasetypes = cell.lb_name.text;
        vc.num = cell.tag;

        [self.navigationController pushViewController:vc animated:YES];
    }
    


}

#pragma mark
#pragma mark - 雪花的动画

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [self stopTimer];
//}

- (void)stopTimer
{
    [self.timer invalidate];
}

- (void)update
{
    steps++;
    //NSLog(@"come here %ld", steps);
    [self snow];
    
}
- (void)snow
{
    //1. 实例化一个雪花的图像视图
    UIImage *image = [UIImage imageNamed:@"雪花"];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    
    //随机在0.1 ~ 0.3 之间缩放
    CGFloat scale =  arc4random_uniform(40) / 100.0;
    //设置雪花的缩放
    imageView.transform = CGAffineTransformMakeScale(scale, scale);
    
    CGSize winSize = self.view.bounds.size;
    //添加到视图中
    CGFloat x = arc4random_uniform(winSize.width);
    CGFloat y = -imageView.bounds.size.height * 0.5;
    
    //NSLog(@"xxxx%lf",x);
    //NSLog(@"yyyy%lf",y);
    
    imageView.center = CGPointMake(x, y);
  //  [[UIApplication sharedApplication].keyWindow addSubview:button];
  //  [self.view addSubview:imageView];
    [[UIApplication sharedApplication].keyWindow addSubview:imageView];
    self.imageView = imageView;
    //动画下落
    [UIView animateWithDuration:20 animations:^{
        
        //移动到终点的位置
        CGFloat toX = arc4random_uniform(winSize.width);
        
        CGFloat toY = winSize.height ;//+ imageView.bounds.size.height * 0.5;
        
        imageView.center = CGPointMake(toX, toY);
        //下落时候旋转
        imageView.transform = CGAffineTransformRotate(imageView.transform, (arc4random_uniform(360)) * M_PI / 180 );
        //NSLog(@"---%lf",imageView.center.y);
        imageView.alpha = 0.3;

    }completion:^(BOOL finished) {
        
        
        [imageView removeFromSuperview];
        
    }];
    
    
}
- (void)getNotification
{
    //结束雪花.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(button:) name:@"button" object:nil];
}
- (void)button:(NSNotification *)sender
{
    NSLog(@"点击了我");
     [self stopTimer];
    [self.imageView removeFromSuperview];
}


- (void)setupUI
{
    self.title = @"首页";
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    
    self.collectionView = collectionView;
    [self.view addSubview:collectionView];
    
    //设置数据源,代理
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:testCellId];
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"WGWheathCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:wheaherCellID];
//    [self.collectionView registerClass:<#(nullable Class)#> forCellWithReuseIdentifier:cycleCellID];
//    [self.collectionView registerNib:[UINib nibWithNibName:<#(nonnull NSString *)#> bundle:nil] forCellWithReuseIdentifier:mingYiCellID];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZWHomeFunctionCell" bundle:nil] forCellWithReuseIdentifier:functonCellID];
    
    //设置背景颜色
    self.collectionView.backgroundColor = [UIColor redColor];
    
    //关闭弹簧
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator =NO;
    self.collectionView.bounces = NO;
    
    //设置增加按钮
    UIButton *rightItem = [UIButton buttonWithType:0];
    rightItem.bounds = CGRectMake(0, 0, 30, 30);
    UIImage *image = [[UIImage imageNamed:@"home_scan"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [rightItem setBackgroundImage:image forState:UIControlStateNormal];
    [rightItem addTarget:self action:@selector(addScan) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:rightItem];
    self.navigationItem.rightBarButtonItem = item;
   // [self setNavigationBar];
}

//#pragma 
//#pragma mark - 设置导航栏
//- (void)setNavigationBar
//{
//    self.navigationController.navigationBar.tintColor = [UIColor yellowColor];
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor yellowColor]}];
//}
//
//#pragma 
//#pragma mark - 添加导航
//- (void)addScan
//{
//    
//    UIViewController *scanVc = [[UIViewController alloc]init];;
//    
//    [self.navigationController pushViewController:scanVc animated:YES];
//}
//
//#pragma mark - dataSource delegate
//
//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//{
//    return 4;
//}
//
//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//{
//    switch (section) {
//        case 0:
//            return 1;
//            case 1:
//            return 1;
//            case 2:
//            return 1;
//            case 3:
//            //return
//            //break;
//            
//        default:
//            return 0;
//            break;
//    }
//}
//
////- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)
////    indexPath
////{
////    if (indexPath.section == 0) {
////        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:wheaherCellID forIndexPath:indexPath];
////        return cell;
////    }
////    
////}
@end
